package com.xj.admin.base.user.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.feilong.core.bean.ConvertUtil;
import com.xj.admin.base.user.entity.TbUser;
import com.xj.admin.base.user.mapper.TbUserMapper;
import com.xj.admin.base.user.service.ITbUserService;
import com.xj.admin.base.userrole.entity.TbRoleUser;
import com.xj.admin.base.userrole.mapper.TbRoleUserMapper;

/**
 * <p>
 * 用户账户表 服务实现类
 * </p>
 *
 * @author xj
 * @since 2016-12-20
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {

	@Autowired
	private TbUserMapper userMapper;
	@Autowired
	private TbRoleUserMapper roleUserMapper;

	@Override
	public Page<TbUser> selectUserList(Page<TbUser> page, String name, Integer deptId,int vip,int parentId,String startTime,String endTime) {
		
		
			if (parentId>0) {
				page.setRecords(userMapper.selectUserListByParent(page, name, deptId,vip,parentId,startTime,endTime));
			} else {
				page.setRecords(userMapper.selectUserList(page, name, deptId,vip,parentId,startTime,endTime));
			}
		
		return page;
	}

	@Override
	@Transactional
	public boolean insertAll(TbUser user) {
		boolean isGet = true;
		//中级或者高级集合
		List<TbUser> all=new ArrayList<TbUser>();
		if (userMapper.insert(user) > 0) {
			TbRoleUser roleUser = new TbRoleUser();
			roleUser.setRId(user.getRole().getId());
			roleUser.setUId(user.getId());
			roleUser.setTCreateTime(new Date(System.currentTimeMillis()));
			if (roleUserMapper.insert(roleUser) > 0) {
					TbUser puser = userMapper.selectById(user.getParentId());
					if(puser!=null) {
						TbUser first=new TbUser();
						ConvertUtils.register(new DateConverter(null), java.util.Date.class);
						try {
							BeanUtils.copyProperties(first, puser);
						} catch (IllegalAccessException | InvocationTargetException e) {
							System.out.println("copybean出错");
							e.printStackTrace();
						}
						
						//第一个上级为高级
						if(first!=null&&first.getVip()==3)
							puser = threeVip(isGet, all, puser,1200d,3);
						//第一个上级为中级
						if(first!=null&&first.getVip()==2) {
							puser = threeVip(isGet, all, puser,1000d,2);
							//等级变更高级
							List<TbUser> selectList=queryByVP(first.getParentId(), 2);
							System.out.println(selectList.size()+"个中级");
							if(selectList.size()>=3) {
								
								TbUser tu = userMapper.selectById(first.getParentId());
								System.out.println(tu.getId());
								tu.setVip(3);
								userMapper.updateById(tu);
							}
						}
							
						//第一个上级为初级
						if (first!=null&&first.getVip()==1){
//							puser = threeVip(isGet, all, puser,800d,1);
							first.setMark(first.getMark()+800);
//							userMapper.updateById(first);
							while(isGet) {
								//所有上级中遇到第一个是中级或高级
								puser = userMapper.selectById(puser.getParentId());
								if(puser!=null&&puser.getVip()==2) {
									puser = threeVip(isGet, all, puser,200d,2);
									break;
								}else if(puser!=null&&puser.getVip()==3) {
									puser = threeVip(isGet, all, puser,400d,3);
									break;
								}
								if(puser==null||puser.getParentId()<=0) {
									break;
								}
							}
							//等级变更中级
							List<TbUser> selectList=queryByVP(first.getId(), 1);
							if(selectList.size()>=5) {
								first.setVip(2);
								userMapper.updateById(first);
								List<TbUser> queryByVP = queryByVP(first.getParentId(), 2);
								if(queryByVP.size()>=3) {
									//上上级变更高级
									TbUser tu = userMapper.selectById(first.getParentId());
									if(tu.getVip()==2) {
										tu.setVip(3);
									}
									if(tu.getVip()==1) {
										//初级  肯定没有5个以上的初级
									}
									userMapper.updateById(tu);
								}
							}else
								userMapper.updateById(first);
						}
						
						
					}
					
				return true;
			}
		}
		return false;
	}

	private List<TbUser> queryByVP(int parentId, int vip) {
		 
		TbUser q=new TbUser();
		q.setParentId(parentId);
		EntityWrapper<TbUser> ew=new EntityWrapper<>(q);
		ew.ge("u_vip", vip);
		return userMapper.selectList(ew);
	}

	private TbUser threeVip(boolean isGet, List<TbUser> all, TbUser puser,Double mark,int viplever) {
		if (puser != null && puser.getVip() == viplever&&viplever>1) {
			
			TbUser first=new TbUser();
			ConvertUtils.register(new DateConverter(null), java.util.Date.class);
			try {
				BeanUtils.copyProperties(first, puser);
			} catch (IllegalAccessException | InvocationTargetException e) {
				System.out.println("copybean出错");
				e.printStackTrace();
			}
			all.add(first);
			while(isGet) {
				
				if(puser.getParentId()>0) 
					puser = userMapper.selectById(puser.getParentId());
				else
					puser=null;
				if(puser != null&&puser.getVip()==viplever) {
					all.add(puser);
					break;
				}else if(puser==null||puser.getParentId()<=0) {
					break;
				}
			}
			if(all.size()==1) {
				System.out.println("1");
				TbUser vip=all.get(0);
				vip.setMark(vip.getMark()+mark);
				userMapper.updateById(vip);
			}
			if(all.size()==2) {
				System.out.println("2");
				TbUser vip=all.get(0);
				vip.setMark(vip.getMark()+mark*0.95);
				userMapper.updateById(vip);
				System.out.println("3");
				TbUser vip1=all.get(1);
				vip1.setMark(vip1.getMark()+mark*0.05);
				userMapper.updateById(vip1);
			}
			if(viplever==3) {
				return puser;
			}
			if(viplever==2) {
				//第二个中级之后还有上级
				System.out.println("222");
				if(puser!=null&&puser.getParentId()>0) {
					while(isGet) {
						System.out.println("333");
						puser = userMapper.selectById(puser.getParentId());
						System.out.println("444");
						if(puser!=null&&puser.getVip()==3) {
							puser = threeVip(isGet, all, puser,200d,3);
							break;
						}
						if(puser==null||puser.getParentId()<=0) {
							break;
						}
					}
					//没遇到第二个中级
				}else {
					System.out.println("555");
					puser = first;
					while(isGet) {
						puser = userMapper.selectById(puser.getParentId());
						if(puser!=null&&puser.getVip()==3) {
							System.out.println(puser.getId()+"=="+puser.getParentId()+"=="+puser.getVip());
							puser = threeVip(isGet, all, puser,200d,3);
							break;
						}
						if(puser==null||puser.getParentId()<=0) {
							break;
						}
					}
				}
				return puser;
			}
	
		}
		return puser;
	}

	@Override
	@Transactional
	public boolean delUser(Integer userId) {
		if (roleUserMapper.deleteByMap(ConvertUtil.toMap("u_id", (Object) userId)) > 0) {
			return userMapper.deleteById(userId) > 0;
		}
		return false;
	}

	@Override
	public TbUser selectUserRole(Map<String, Object> parameter) {
		return userMapper.selectUserRole(parameter);
	}

	@Override
	public boolean updateAll(TbUser user) {
		TbUser baseUser = userMapper.selectById(user.getId());
		if (userMapper.update(user, new EntityWrapper<TbUser>(baseUser)) > 0) {
			TbRoleUser roleUser = new TbRoleUser();
			roleUser.setRId(user.getRole().getId());
			roleUser.setUId(user.getId());
			roleUser.setTCreateTime(new Date(System.currentTimeMillis()));
			return roleUserMapper.update(roleUser, new EntityWrapper<TbRoleUser>(new TbRoleUser(user.getId()))) > 0;
		}

		return false;
	}
	public static void main(String[] args) {
//		TbUser puser=new TbUser();
//		puser.setId(22);
//		TbUser one =new TbUser();
//		try {
//			ConvertUtils.register(new DateConverter(null), java.util.Date.class);
//			BeanUtils.copyProperties(one, puser);
//		} catch (IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		puser.setId(33);
//		System.out.println(one.getId());
//		System.out.println(puser.getId());
		
		
//		if(puser.getId()==22) {
//			
//			int i=0;
//			while(true) {
//				puser.setId(33);
//				System.out.println(puser.getId());
//				if(i==2) {
//					break;
//				}
//				i++;
//			}
			
//		}
	
	}

}
